package com.example.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ware.annotation.Token;
import com.example.ware.model.po.Admin;
import com.example.ware.model.po.Employees;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.service.AdminService;
import com.example.ware.utils.Ujwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
@RequestMapping("/Admin")
public class AdminController {

    @Autowired
    AdminService adminServices;

    @Autowired
    Ujwt ujwt;

    @Resource
    RedisTemplate redisTemplate;


    //    管理员登录接口
    @PostMapping("/login")
    public ResponseData login(@RequestBody Admin admin) {

        if (admin.getUsername() == null && admin.getPassword() == null) {
            return new ResponseData(null, false, "账号或密码为空", 201);
        }

        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("username", admin.getUsername());
        qw.eq("password", admin.getPassword());
        Admin e = adminServices.getOne(qw);


        if (e.getAuth() == 0) {
            return new ResponseData(null, false, "该用户已经封禁", 404);
        }

        if (e != null) {
            String token = ujwt.gettoken1(e);
//            redisTemplate.opsForHash().put(e.getId(),token,e);
//            redisTemplate.expire(token,100, TimeUnit.SECONDS);
//            第一个参数为redis的key，二为map的key，三为map的value
//            redisTemplate.opsForValue().set(token,e,1000*60, TimeUnit.SECONDS);
//            redisTemplate.opsForValue().set("Admin_" + token, e, 100, TimeUnit.DAYS); //1天过期时间
            redisTemplate.opsForValue().set("Admin_" + token, e,  100L * 24 * 3600, TimeUnit.DAYS); //1天过期时间


            return new ResponseData(token, true, "登录成功", 200);

        } else {
            return new ResponseData("null", false, "账号或密码错误,请联系管理员", 404);
        }
    }


    //    登出
    @PostMapping("/logout")
    public ResponseData logout(@RequestHeader String token) {
        Boolean delete = redisTemplate.delete("Admin_" + token);

        if (delete) {
            return new ResponseData(null, true, "成功退出登录", 200);
        } else {
            return new ResponseData(null, false, "退出登录失败", 201);
        }
    }


    //    搜索人员列表
    @Token
    @GetMapping("/list")
    public ResponseData getLiost(String name, int auth, Page p) {
        Page<Admin> adminPage = new Page<>();
        if (name.equals("")) {
            QueryWrapper<Admin> qw = new QueryWrapper<>();
            qw.eq("auth", auth).or().eq("auth", 0);
            adminPage = adminServices.page(p, qw);
        } else {
            QueryWrapper<Admin> qw = new QueryWrapper<>();
            qw.like("name", name);
            qw.eq("auth", auth);
            adminPage = adminServices.page(p, qw);
        }

        if (adminPage != null) {
            return new ResponseData(adminPage, true, "获取成功", 200);
        } else {
            return new ResponseData(adminPage, true, "获取成功", 200);
        }
    }

    //    创建特殊用户
    @PostMapping("add")
    @Token
    public ResponseData add(@RequestBody Admin a) {
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("wid", a.getWid());
        if (adminServices.list(qw).size() != 0) {
            return new ResponseData(null, false, "已经存在该区域管理员", 202);
        }

        a.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        boolean save = adminServices.save(a);

        if (save) {
            return new ResponseData(null, true, "添加成功", 200);
        } else {
            return new ResponseData(null, false, "添加失败", 201);
        }
    }


    //修改用户
    @PutMapping("update")
    @Token
    public ResponseData update(@RequestBody Admin a) {
        UpdateWrapper<Admin> qw = new UpdateWrapper<>();
        qw.eq("id", a.getId());
        qw.set("auth", a.getAuth());

        boolean save = adminServices.update(qw);

        if (save) {
            return new ResponseData(null, true, "修改成功", 200);
        } else {
            return new ResponseData(null, false, "修改失败", 201);
        }
    }

    @GetMapping("info")
    @Token
    public ResponseData getInfo(@RequestHeader String token){
        Admin admin = (Admin)redisTemplate.opsForValue().get("Admin_" + token);
        return new ResponseData(admin.getAuth(),true,"成功获取",200);
    }
}
